#include "stdafx.h"
#include "CommonFunction.h"

BOOL getListNonRecursive(TCHAR* pPath, CStringArray* pLstFiles)
{
	/* Declare variables */
#ifdef DEBUG
	DWORD		 dTime		= GetTickCount();
#endif
	int			 nTotal		= 0;
	BOOL		 bWorking	= FALSE;
	CString		 szPath		= EMPTY_STRING;
	CFileFind	 finder;
	CStringArray lstFolder;

	/* Check parameter */
	if (!pLstFiles || !pPath)
	{
		return TRUE;
	}

	/* Clear all items */
	pLstFiles->RemoveAll();
	/* Build directory path */
	szPath.Format(_T("%s%s"), pPath, ALL_FILES);
	/* Add item to list folder */
	lstFolder.Add(szPath);
	nTotal += 1;

	/* Loop all list folder */
	for (int i = 0; i < nTotal; i++)
	{
		bWorking = finder.FindFile(lstFolder[i]);

		while (bWorking)
		{
			bWorking = finder.FindNextFileW();

			if (finder.IsDots())
			{
				continue;
			}

			if (finder.IsDirectory())
			{
				/* Build directory path */
				szPath.Format(_T("%s%s"), finder.GetFilePath(), ALL_FILES);
				/* Add item to list folder */
				lstFolder.Add(szPath);
				nTotal += 1;

				continue;
			}

			/* Build file path */
			szPath.Format(_T("%s%s"), finder.GetFilePath(), finder.GetFileName());
			pLstFiles->Add(szPath);
		}
	}

	finder.Close();
#ifdef DEBUG
	dTime = GetTickCount() - dTime;
	TRACE(_T("\nGet list files successfully in %lu ml - %d\n"), dTime, pLstFiles->GetCount());
#endif

	return FALSE;
}

BOOL getListRecursive(TCHAR* pPath, CStringArray* pLstFiles)
{
	BOOL		 bWorking = FALSE;
	CString		 szPath = EMPTY_STRING;
	CFileFind	 finder;

	/* Build directory path */
	szPath.Format(_T("%s%s"), pPath, ALL_FILES);

	bWorking = finder.FindFile(szPath);

	while (bWorking)
	{
		bWorking = finder.FindNextFileW();

		if (finder.IsDots())
		{
			continue;
		}

		if (finder.IsDirectory())
		{
			/* Get list file in folder */
			getListRecursive((TCHAR*)(LPSTR)finder.GetFilePath().AllocSysString(), pLstFiles);

			continue;
		}

		/* Build file path */
		szPath.Format(_T("%s%s"), finder.GetFilePath(), finder.GetFileName());
		pLstFiles->Add(szPath);
	}

	return FALSE;
}
